//功能：喜帖调数据
var url = window.location.href.split('_');
var url_length = url.length;

var invitation_id = url[url_length-1];
console.log(invitation_id);
var informat = new Object();
$.ajax({
    type: "GET",
    url:'api/getinvitation/'+invitation_id,
    success: function(data) {
        var date = new Date;
        var day = date.toLocaleDateString();
        $("#theme_"+invitation_id).find("#date_now").text("现在是："+day);
    	informat = data.result[0];
        console.log(informat);
        $("#theme_"+invitation_id).find(".picture_main").attr("src",informat.picture_main);
       
        // if ($("#theme_"+invitation_id) == undefined) {
        //      var sourcehtml = '<label id="playbox" class="on" onclick="mp3_play()">\
        //         <audio id="audio" autoplay="autoplay" loop="loop">\
        //             <source src="../mp3/'+informat.music_id+'.mp3" type="audio/mp3">\
        //         </audio>\
        //     </label>'
        //     $(".nav_top").append(sourcehtml);
        // }
        if ($("#theme_"+invitation_id) != '') {
                 $(".nav_top label").remove();
                 var getsourcehtml = '<label id="playbox" class="on" onclick="mp3_play()">\
                    <audio id="audio" autoplay="autoplay" loop="loop">\
                        <source src="'+informat.music_url+'" type="audio/mp3">\
                    </audio>\
                </label>'
                $(".nav_top").append(getsourcehtml);
        }
        
        $("#theme_"+invitation_id).find(".man_name").text(informat.boy_name);
        $("#theme_"+invitation_id).find(".women_name").text(informat.girl_name);
        $("#theme_"+invitation_id).find("#invit_letter").text(informat.invit_letter);
        $("#theme_"+invitation_id).find("#wedding_time").text("婚礼时间："+informat.wedding_time);
        $("#theme_"+invitation_id).find("#wedding_add").text("婚礼地点："+informat.wedding_address);
        $("#theme_"+invitation_id).find("#traffic_way").html("交通指南："+informat.traffic_way);
        $("#theme_"+invitation_id).find("#boy_tel").text(informat.boy_tel);
        $("#theme_"+invitation_id).find("#girl_tel").text(informat.girl_tel);
        $("#theme_"+invitation_id).find("#video source").attr("src",informat.video);
        $.each(informat.pictures,function(i,val){
            var tpl = '<img src="'+val.dir+'">';
            $("#picture").append(tpl);
        })
        $("#thanks_"+invitation_id).find("#thanks_word").text(informat.thanks);

    }
}); 

function mp3_play(){
    if ($('#playbox').hasClass('on')){
    	$('#playbox').attr('class','');
        $('#playbox audio').get(0).pause();
    }else{
    	$('#playbox').attr('class','on');
        $('#playbox audio').get(0).play();
    }
}
function select_join(obj){
	$(".join_btns a").each(function(){
	    $(this).removeClass("active");
	});
    $(obj).addClass("active");
	$("#is_join").val($(obj).attr("data"));
	if ($("#is_join").val()== 1) {
		$("#join_num").show();
		
	};
	if ($("#is_join").val()== 0){
		$("#join_num").hide();
	}

}

function themeSub(){
    var bless = new Object();
    bless.invitation_id = invitation_id;
    bless.name = $("#userName").val();
    bless.phone = $("#telephone").val();
    bless.blessing = $('#content').val();

    var attend =new Object();
    attend.invitation_id = invitation_id;
    attend.name = $("#userName").val();
    attend.phone = $("#telephone").val();
    attend.blessing = $('#content').val();
    attend.number = $('#join_num').val();
    if ($("#is_join").val()== 1) {
        $.ajax({
            type: "POST",
            url:'api/saveattend',
            data:attend,
            success: function(data) {
                attend.name = $("#userName").val('');
                attend.phone = $("#telephone").val('');
                attend.blessing = $('#content').val('');
                attend.number = $('#join_num').val('');
                $("#thanks").text("谢谢您的祝福！");
                $("#thanks").show();
                setTimeout(function(){$("#thanks").hide}, 1000);
            },
            error: function(request) {
                $("#thanks").text("提交失败，程序员正在玩命修复中~~~");
                $("#thanks").show();
                setTimeout(function(){$("#thanks").hide}, 1000);
            }
        }); 
    };
    if ($("#is_join").val()== 0){
        $.ajax({
            type: "POST",
            url:'api/savebless',
            data: bless,
            success: function(data) {
                attend.name = $("#userName").val('');
                attend.phone = $("#telephone").val('');
                attend.blessing = $('#content').val('');
                attend.number = $('#join_num').val('');
                $("#thanks").text("谢谢您的祝福！");
                $("#thanks").show();
                setTimeout(function(){$("#thanks").hide()}, 1000);
            },
            error: function(request) {
                attend.name = $("#userName").val('');
                attend.phone = $("#telephone").val('');
                attend.blessing = $('#content').val('');
                attend.number = $('#join_num').val('');
                $("#thanks").text("提交失败，程序员正在玩命修复中~~~");
                $("#thanks").show();
                setTimeout(function(){$("#thanks").hide()}, 1000);
            }
        }); 
    }
}


